var app = getApp()
var util = require('../../utils/util.js')
var md5 = require('../md5.js');
Page({
  data: {

  },
  submit: function () {
    this.setData({
      showModal: true
    })
  },
  onPullDownRefresh: function(){
    wx.stopPullDownRefresh({
      complete: (res) => {},
    });
  },
  //底部导航跳转
  toAiIndex: function () {
    util.toAiIndex()
  },
  toContactsIng: function () {
    util.toContactsIng()
  },
  toProductRevision: function () {
    util.toProductRevision()
  },
  toMy: function () {
    util.toMy()
  },
  toBadge: function () {
    util.toBadge()
  },
  preventTouchMove: function () {

  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  toChangeShow: function () {
    var that = this
    that.setData({
      luzsshow: false
    })
  },
  sc: function (e) {
    var that = this
    var productid = e.currentTarget.dataset.tagid
    console.log('productid=' + productid)
    var keyword = that.data.keyword
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if (!(exhibitionid=='0' && u=='0')) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/products/collect'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["productid"] = productid
      k["u"] = u
      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'productid' + productid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          //从新加载数据刷新
          if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
            && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
            //默认最新（调用pico接口）
            var timestamp = Date.parse(new Date());
            timestamp = timestamp / 1000;
            var url = app.globalData.picoaddress + '/attendee/products/list'
            var k = {}
            k["v"] = '1.0.0'
            k["client"] = 'ios'
            k["timestamp"] = timestamp
            k["lan"] = 'cn'
            k["exhibitionid"] = exhibitionid
            k["u"] = u
            var sign = ''
            if (!keyword || keyword == '') {
              sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
            } else {
              k["keyword"] = keyword
              sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'keyword' + keyword + 'timestamp' + timestamp + app.globalData.picotoken
            } 
            console.log(sign)
            var curb64 = md5.zhMD5(sign)
            console.log(curb64)
            k["sign"] = curb64
            wx.request({
              header: {
                'content-type': 'application/x-www-form-urlencoded',
              },
              url: url,
              method: 'post',
              data: k,
              success(res) {
                console.log(res)
                var mesg = res.data
                console.log('mesg=' + mesg)
                var curdata = mesg.data
                console.log('curdata=', curdata)
                that.setData({
                  list: curdata
                });
              }, fail: res => {
                console.log(res, "error")
              }

            })

          }
        }, fail: res => {
          console.log(res, "error")
        }
      })
    } else {
      wx.navigateTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  go: function (e) {
    var that = this;
    //获取关键词
    var keyword = e.detail.value
    //var keyword = that.data.inputVal
    that.setData({
      keyword: keyword
    });
    console.log('keyword=' + keyword)
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/products/list'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["keyword"] = keyword
      k["u"] = u
      var sign = ''
      if (!keyword || keyword == '') {
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      }else{
        sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'keyword' + keyword + 'timestamp' + timestamp + app.globalData.picotoken
      } 
      var curb64 = md5.zhMD5(sign)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          console.log('curdata=', curdata)
          that.setData({
            list: curdata
          });
        }, fail: res => {
          console.log(res, "error")
        }

      })

    } else {
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  onLoad: function (options) {
    var that = this
    //设置语言0:中文，1：英文
    var setlanguage = wx.getStorageSync('setlanguage')
    if ((wx.getStorageSync('setlanguage') || !"".trim() == wx.getStorageSync('setlanguage'))) {
      this.setData({
        setlanguage: wx.getStorageSync('setlanguage')
      })
      if (setlanguage == 1) {
        wx.setNavigationBarTitle({
          title: 'Product list'
        })
      } else {
        wx.setNavigationBarTitle({
          title: '产品列表'
        })
      }
    } else {
      wx.setStorageSync('setlanguage', 0);
      this.setData({
        setlanguage: 0
      })
    }
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if ((wx.getStorageSync('exhibitionid') || !"".trim() == wx.getStorageSync('exhibitionid'))
      && (wx.getStorageSync('u') || !"".trim() == wx.getStorageSync('u'))) {
    //默认最新（调用pico接口）
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    var url = app.globalData.picoaddress + '/attendee/products/list'
    var k = {}
    k["v"] = '1.0.0'
    k["client"] = 'ios'
    k["timestamp"] = timestamp
    k["lan"] = 'cn'
    k["exhibitionid"] = exhibitionid
    k["u"] = u

    var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
    console.log(sign)
    var curb64 = md5.hexMD5(sign)
    console.log(curb64)
    k["sign"] = curb64
    wx.request({
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      url: url,
      method: 'post',
      data: k,
      success(res) {
        console.log(res)
        var mesg = res.data
        console.log('mesg=' + mesg)
        var curdata = mesg.data
        console.log('curdata=', curdata)
        that.setData({
          list: curdata
        });
      }, fail: res => {
        console.log(res, "error")
      }
    })
   }else{
      wx.redirectTo({
        url: '../signInbyPhone/signIn'
    })
   }
  },
  luzs: function (e) {
    var that = this;
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var toid = e.currentTarget.dataset.contactid
    var u = wx.getStorageSync('u')
    if (!(exhibitionid == '0' && u == '0')) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/exhibitors/contactme'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["toid"] = toid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + 'toid' + toid + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          if (mesg.code == 200) {
            that.setData({
              luzsshow: true
            })    
          } else {
            wx.showToast({
              title: mesg.message,
              icon: 'none',
              duration: 1500
            })
          }

        },
        fail: res => {
          console.log(res, "error")
        }

      })
      
    } else {
      wx.navigateTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  hqmp: function (e) {
    var that = this;
    var curid = e.currentTarget.dataset.tagid+''
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var exhibitorid = (curid.split('-'))[0]
    var toid = (curid.split('-'))[1]
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if (!(exhibitionid == '0' && u == '0')) {
      //默认最新（调用pico接口）
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      var url = app.globalData.picoaddress + '/attendee/exhibitors/getexhibitorcard'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["exhibitorid"] = exhibitorid
      k["u"] = u

      var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'exhibitorid' + exhibitorid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: url,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          if (mesg.code == 200) {
            wx.showToast({
              title: '展商名片成功添加到通讯录',
              icon: 'none',
              duration: 1500
            })
            wx.navigateTo({
              url: '../pageDetail/pageDetails?attendeeid=' + toid,
            })

          } else {
            wx.showToast({
              title: mesg.message,
              icon: 'none',
              duration: 1500
            })
          }
        },
        fail: res => {
          console.log(res, "error")
        }

      })


    } else {
      wx.navigateTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  yjzs: function (e) {
    var that = this;
    //获取缓存展会id和用户id存在则获取最新联系人列表
    var exhibitionid = wx.getStorageSync('exhibitionid')
    var toid = e.currentTarget.dataset.contactid
    //  var toid = that.data.toid
    // var fullname = that.data.fullname
    var u = wx.getStorageSync('u')
    console.log('exhibitionid=' + exhibitionid)
    console.log('u=' + u)
    if (!(exhibitionid == '0' && u == '0')) {
      var timestamp = Date.parse(new Date());
      timestamp = timestamp / 1000;
      //==============加载联系人fullname=========================
      var cururl = app.globalData.picoaddress + '/attendee/contacts/detail'
      var k = {}
      k["v"] = '1.0.0'
      k["client"] = 'ios'
      k["timestamp"] = timestamp
      k["lan"] = 'cn'
      k["exhibitionid"] = exhibitionid
      k["attendeeid"] = toid
      k["u"] = u
      var sign = app.globalData.picotoken + 'attendeeid' + toid + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + app.globalData.picotoken
      console.log(sign)
      var curb64 = md5.hexMD5(sign)
      console.log(curb64)
      k["sign"] = curb64
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        url: cururl,
        method: 'post',
        data: k,
        success(res) {
          console.log(res)
          var mesg = res.data
          console.log('mesg=' + mesg)
          var curdata = mesg.data
          var fullname = curdata.fullname
          var url = app.globalData.picoaddress + '/attendee/exhibitors/appointment'
          var k = {}
          k["v"] = '1.0.0'
          k["client"] = 'ios'
          k["timestamp"] = timestamp
          k["lan"] = 'cn'
          k["exhibitionid"] = exhibitionid
          k["toid"] = toid
          k["u"] = u

          var sign = app.globalData.picotoken + 'clientiosexhibitionid' + exhibitionid + 'timestamp' + timestamp + 'toid' + toid + app.globalData.picotoken
          console.log(sign)
          var curb64 = md5.hexMD5(sign)
          console.log(curb64)
          k["sign"] = curb64
          wx.request({
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            url: url,
            method: 'post',
            data: k,
            success(res) {
              console.log(res)
              var mesg = res.data
              console.log('mesg=' + mesg)
              if (mesg.code == 200) {
                wx.showToast({
                  title: '约见展商消息已成功发出，等待展商确认',
                  icon: 'none',
                  duration: 2000
                })
                wx.navigateTo({
                  url: '../contactsModule/chat?attendeeid=' + toid + '&nickname=' + fullname,
                })

              } else {
                wx.showToast({
                  title: mesg.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            },
            fail: res => {
              console.log(res, "error")
            }

          })

        },
        fail: res => {
          console.log(res, "error")
        }
      })

    } else {
      wx.navigateTo({
        url: '../signInbyPhone/signIn'
      })
    }
  },
  choiceWordindex: function (event) {
    let wordindex = event.currentTarget.dataset.wordindex;
    this.setData({
      toView: wordindex,
    })
  },
  toDetail: function (e) {
    var that = this;
    var tagid = e.currentTarget.dataset.tagid;
    console.log('tagid', tagid)
    wx.navigateTo({
      url: '../productModule/productDetails?productid=' + tagid,
    })
  },
})